﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.CommonObject;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Client.InfoWms;
using Zwms.Client.Helper;

namespace Zwms.Client.InWms
{
    public partial class InPlanBillDetailForm : BaseForm
    {
        public BilInPlanBillDetail CurrentInPlanBillDetail { get; set; }
        private EnumOpenType OpenType { get; set; }
        private int _spec = 1;
        private string _singleGoodsCode = "";

        public InPlanBillDetailForm(EnumOpenType opentype, string sInPlanBillCode, string sGoodsCode, string sGoodsBatch)
        {
            InitializeComponent();
            CurrentInPlanBillDetail = new BilInPlanBillDetail();

            this.OpenType = opentype;
            if (opentype == EnumOpenType.New)
            {
                ServiceReferenceZwms.ServiceZwmsClient client 
                    = new ServiceReferenceZwms.ServiceZwmsClient();
                BilInPlanBill queryRecord = new BilInPlanBill();
                queryRecord.Id = sInPlanBillCode;
                queryRecord = client.QueryInPlanBill(queryRecord);
                CurrentInPlanBillDetail.IBPlanCode = queryRecord.Id;
                dpuSendDate.SetDate(client.GetCurrentSystemTime());
            }
            else if (opentype == EnumOpenType.Open)
            {
                tbsGoodsCode.SetEditState(false);
                //通过服务读取'入库计划明细'的实例
                ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
                BilInPlanBillDetail record = new BilInPlanBillDetail();
                record.IBPlanCode = sInPlanBillCode;
                record.GoodsCode = sGoodsCode;
                record.GoodsBatch = sGoodsBatch;
               
                CurrentInPlanBillDetail =  client.QueryInPlanBillDetail(record);
                _spec = CurrentInPlanBillDetail.GoodsSpec;
                _singleGoodsCode = CurrentInPlanBillDetail.SingleGoodsCode;
            }

            InitControl();
        }

        #region 私有函数
        private void InitControl()
        {
            if (this.OpenType == EnumOpenType.New)
            {

            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                LoadDetail(CurrentInPlanBillDetail);
            }

        }

        private void LoadDetail(BilInPlanBillDetail record)
        {
            this.dpuSendDate.SetDate(CommonHelper.GetDatetimeFromString(record.SendDate));
            this.tbsGoodsCode.SetTextBoxContent(record.GoodsName);
            this.tbsGoodsCode.Tag = record.GoodsCode;
            this.nudGoodsPackageQty.Value = record.GoodsPackageQty;
            this.nudGoodsPieceQty.Value = record.GoodsPieceQty;
            this.nudGoodsQty.Value = record.GoodsQty;
            this.tbPOCode.Text = record.POCode;
            this.tbSendBillCode.Text = record.SendBillCode;
            this.nudPiecePrice.Value = record.PiecePrice;
            this.tbRemark.Text = record.Remark;
        }

        public BilInPlanBillDetail GetDetail()
        {
            if (string.IsNullOrWhiteSpace(tbsGoodsCode.GetTextBoxContent()))
            {
                return null;
            }
           
            {
                CurrentInPlanBillDetail.GoodsCode = tbsGoodsCode.Tag.ToString();
                CurrentInPlanBillDetail.GoodsName = tbsGoodsCode.GetTextBoxContent();
                CurrentInPlanBillDetail.GoodsSpec = _spec;
                CurrentInPlanBillDetail.PiecePrice = nudPiecePrice.Value;
                CurrentInPlanBillDetail.SingleGoodsCode = _singleGoodsCode;
                CurrentInPlanBillDetail.POCode = tbPOCode.Text;
                if (dpuSendDate.SelectDateTime != null)
                {
                    CurrentInPlanBillDetail.SendDate = dpuSendDate.SelectDateTime;
                }

                CurrentInPlanBillDetail.SendBillCode = tbSendBillCode.Text;
                CurrentInPlanBillDetail.GoodsPackageQty = Convert.ToInt32(nudGoodsPackageQty.Value);
                CurrentInPlanBillDetail.GoodsPieceQty = Convert.ToInt32(nudGoodsPieceQty.Value);
                CurrentInPlanBillDetail.GoodsQty = Convert.ToInt32(nudGoodsQty.Value);
                CurrentInPlanBillDetail.ReceiveGoodsPackageQty = 0;
                CurrentInPlanBillDetail.ReceiveGoodsPieceQty = 0;
                CurrentInPlanBillDetail.ReceiveGoodsQty = 0;

                return CurrentInPlanBillDetail;
            }
        }

        private void CalculateGoodsQty()
        {
            nudGoodsQty.Value = nudGoodsPackageQty.Value * _spec + nudGoodsPieceQty.Value;
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            bool bRet = true;
            if (string.IsNullOrWhiteSpace( tbsGoodsCode.GetTextBoxContent()))
            {
                bRet = false;
                tbsGoodsCode.SetTextBGColor(Color.Yellow);
            }

            if (!bRet)
            {
                return;
            }
           BilInPlanBillDetail record =  GetDetail();
           if (record == null)
           {
               return;
           }
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
            if (this.OpenType == EnumOpenType.New)
            {
                if (client.AddInPlanBillDetail(record))
                {
                    MessageBox.Show("成功！");
                }
                else
                {
                    MessageBox.Show("失败，请检查是否已添加此商品，如需区别同类商品，请添加批次信息！");
                    return;
                }
            }
            else if (this.OpenType == EnumOpenType.Open)
            {
                if (client.UpdateInPlanBillDetail(record))
                {
                    MessageBox.Show("成功！");
                }
                else
                {
                    MessageBox.Show("失败，请检查是否已添加此商品，如需区别同类商品，请添加批次信息！");
                    return;
                }
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.tbsGoodsCode.SetTextBoxContent(string.Empty);
            this.tbsGoodsCode.Tag = string.Empty;
            this.tbPOCode.Text = "";
            this.nudGoodsPackageQty.Value = 1;
            this.nudGoodsPieceQty.Value = 1;
            this.nudGoodsQty.Value = 1;
            this.tbRemark.Text = string.Empty;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        //选择货物
        private void tbsGoodsCode_SelectButtonClick(object sender, EventArgs e)
        {
            GoodsWnd uc = new GoodsWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择货物");
            uc.SetQueryState();
            if (window.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbsGoodsCode.SetTextBoxContent(uc.SelectedRecord.GoodsName);
                tbsGoodsCode.Tag = uc.SelectedRecord.Id;
                _spec = uc.SelectedRecord.Specs;
                _singleGoodsCode = uc.SelectedRecord.SingleGoodsCode;
                CalculateGoodsQty();
            }
        }

        private void nudQty_ValueChanged(object sender, EventArgs e)
        {
            CalculateGoodsQty();
        }
        #endregion

       
    }
}
